package com.wwcom.darteagle;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.wwcom.darteagle.data.DartEagleDatabase;
import com.wwcom.darteagle.model.DartStats;
import com.wwcom.darteagle.model.Player;
import com.wwcom.darteagle.util.Settings;
import com.wwcom.darteagle.util.Settings.StatsSettings;


public class PlayerStatsSetupActivity extends Activity {
	
    private PlayerArrayAdapter adapterPlayerList;
    private ListView playerList;    
	
	private List<Player> players;
	
    /** Called when the activity is first created. */	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
	    //layout
        setContentView(R.layout.playerstats_players);                        
               	
    	players= new ArrayList<Player>();
    }
    
    
    @Override
    public void onResume() {
    	super.onResume();
    	DartEagleDatabase db= new DartEagleDatabase(this);             	

        //autocomplete textview
    	List<Player> dbPlayers= db.getPlayers();
    	List<String> playerNames= new ArrayList<String>(dbPlayers.size());
    	for (Player p : dbPlayers) {
    		playerNames.add(p.getName());
    	}    	
        db.close();
        ArrayAdapter <CharSequence> adapter =
      		  new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item,playerNames.toArray(new String[]{}) );
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner playerNameSpinner = (Spinner) findViewById(R.id.new_player_name);
        playerNameSpinner.setAdapter(adapter);
    	
        //players list
        Settings.loadSettings(this);
        players= StatsSettings.getPlayers();
        adapterPlayerList = new PlayerArrayAdapter(this, players);
        playerList= (ListView)findViewById(R.id.player_list);
        playerList.setAdapter(adapterPlayerList);
        
        //populate stats list with a list of options defined in DartStats class
        Spinner selectedStat= ((Spinner) findViewById(R.id.playerstats_selectedstat));
        ArrayAdapter <CharSequence> spinnerAdapter =
        		  new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item );
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        for (int resId : DartStats.STATS_NAMES) {
            String label= this.getString(resId);
            label= label.substring(0,label.length()-1);  //get rid of :        	
        	spinnerAdapter.add(label);
        }
        selectedStat.setAdapter(spinnerAdapter);
        Log.d("PlayerStatsSetupActivity","Selected stat: "+StatsSettings.getSelectedStat());
        selectedStat.setSelection(StatsSettings.getSelectedStat());
        spinnerAdapter.notifyDataSetChanged();
        		 

    }
    
    @Override
    public void onPause() {
    	super.onPause();
        Spinner selectedStat= ((Spinner) findViewById(R.id.playerstats_selectedstat));
		
		StatsSettings.updateSettings(players,(int) selectedStat.getSelectedItemId());
		Settings.saveSettings(this);
    }
    

    //buttons
    public void onClickPlayerAdd(View v) {
    	Spinner playerNameSpinner = (Spinner)findViewById(R.id.new_player_name);    	
    	String playerName= (String)playerNameSpinner.getSelectedItem();
    	Player player= new Player(playerName);
    	    	
		players.add(player);		
    	adapterPlayerList.notifyDataSetChanged();
    	playerList.smoothScrollToPosition(playerList.getChildCount());
    }
    
	public void onClickPlayerRemove(View v) {
		Player player= (Player)v.getTag();
		players.remove(player);
    	adapterPlayerList.notifyDataSetChanged();
    }    
        
	public void onClickViewStats(View v) {
		if (players.size() < 1) {
			Toast t = Toast.makeText(this, getString(R.string.playerstats_add_one_player), Toast.LENGTH_SHORT);
			t.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
			t.show();
			return;
		}
			
		Intent i= new Intent(this,PlayerStatsActivity.class);
		List<String> playerNames= new ArrayList<String>(players.size());
		for (Player p : players) {
			playerNames.add(p.getName());
		}
		i.putExtra(PlayerStatsActivity.PLAYER_NAMES, TextUtils.join("#,#",playerNames));
        Spinner selectedStat= ((Spinner) findViewById(R.id.playerstats_selectedstat));                
		i.putExtra(PlayerStatsActivity.STAT_ID, selectedStat.getSelectedItemId());
	    startActivity(i);
	}


    // array adapters
	private class PlayerArrayAdapter extends ArrayAdapter<Player> {
    	private final List<Player> list;
    	private final Context context;

    	public PlayerArrayAdapter(Context context, List<Player> list) {
    		super(context, R.layout.matchsetup_player_name_listview_row, list);
    		this.context = context;
    		this.list = list;
    	}

    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		View view = null;
    		if (convertView == null) {
    			LayoutInflater inflator = ((Activity) context).getLayoutInflater();
    			view = inflator.inflate(R.layout.matchsetup_player_name_listview_row, null);    			
    		} else {
    			view = convertView;
    		}
    		Player player= list.get(position);
    		((TextView)view.findViewById(R.id.player_name)).setText(player.getName());
    		((Button)view.findViewById(R.id.player_position)).setTag(player);
    		return view;
    	}
    	
    }
     
}